/**
 *    ------------------------------------------------------------------------------------------
 *    PExpenses version 1.0
 *    ------------------------------------------------------------------------------------------
 *    PExpenses is a financial tool to track personal expenses with a quick and user friendly gui.
 *    ------------------------------------------------------------------------------------------
 *    Author			: M. Kaisar-Ul-Haque
 *    E-mail			: m.kaisar@gmail.com
 *    Author Blog	: http://kaisar-haque.blogspot.com
 *    Project Web : http://code.google.com/p/pexpenses
 *    Created		: June 23, 2007
 *    ------------------------------------------------------------------------------------------
 *    Copyright (C) 2007 M. Kaisar-Ul-Haque <m.kaisar@gmail.com>
 *    ------------------------------------------------------------------------------------------
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation; either version 2
 *    of the License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *    ------------------------------------------------------------------------------------------
*/

/*
 * RecordStoreManager.java
 *
 * Created on June 24, 2007, 12:13 AM
 */

package kaisar;

/**
 *
 * @author Kaisar
 */
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;


public class RecordStoreManager
{
    private Alert alert;
    private RecordStore recordstore = null;
    
    /** Creates a new instance of RecordStoreManager */
    public RecordStoreManager(String storeName)
    {
        open(storeName);
    }

    public String read(int recordId)
    {
        try
        {
            byte[] recData = new byte[64];
            int len;

            if (recordstore.getRecordSize(recordId) > recData.length)
                recData = new byte[recordstore.getRecordSize(recordId)];

            len = recordstore.getRecord(recordId, recData, 0);
            String s = new String(recData, 0, len);

            return s;
        }
        catch (Exception error)
        {
            System.err.println(error.toString());
        }
        return null;
    }

    public String readAll()
    {
        StringBuffer buf = new StringBuffer();
        byte[] recData = new byte[64];
        int len;

        try
        {
            for (int i = 1; i <= recordstore.getNumRecords(); i++)
            {
                if (recordstore.getRecordSize(i) > recData.length)
                    recData = new byte[recordstore.getRecordSize(i)];

                len = recordstore.getRecord(i, recData, 0);
                buf.append((new String(recData, 0, len)) + "\n");
            }
            
            return buf.toString();
        }
        catch(Exception error)
        {
            System.err.println(error.toString());
        }
        return null;
    }
    
    public boolean write(int recordId, String data)
    {
        try
        {
            byte[] recData = data.getBytes();
            recordstore.setRecord(recordId, recData, 0, recData.length);
            
            return true;
        }
        catch (Exception error)
        {
            System.err.println(error.toString());
        }
        return false;
    }

    public boolean append(String outputData)
    {
        try
        {
            byte[] recData = outputData.getBytes();
            recordstore.addRecord(recData, 0, recData.length);
            return true;
        }
        catch (Exception error)
        {
            System.err.println(error.toString());
        }
        return false;
    }

    public RecordStore getRecordStore()
    {
        return recordstore;
    }
    
    public boolean open(String storeName)
    {
        try
        {
            recordstore = RecordStore.openRecordStore(storeName, true);
            return true;
        }
        catch (Exception error)
        {
            System.err.println(error.toString());
        }
        return false;
    }
    
    public boolean close()
    {
        try
        {
            recordstore.closeRecordStore();
            return true;
        }
        catch (Exception error)
        {
            System.err.println(error.toString());
        }
        return false;
    }
}
